package cn.edu.qlnu.xky.test.controller;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

import cn.edu.qlnu.xky.test.model.User;
import cn.edu.qlnu.xky.test.service.UserService;

/**
 * Servlet implementation class TestController
 */
public class UserController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserController() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("能否在后台看到我？我是UserController的代码");
		request.setCharacterEncoding("utf-8");
		//写获取页面用户名和密码的代码，并且去数据库中查询
		String userName = request.getParameter("userName");
		String userPsw = request.getParameter("userPsw");
		User u = (new UserService()).SearchByNamePsw(userName, userPsw);
		
		if(u!=null) {
			System.out.println("UserController's 用户登录成功");
			request.getSession().setAttribute("curUser", u);
			request.getRequestDispatcher("/main.jsp").forward(request, response);
		}else {
			System.out.println("UserController's 用户登录不成功，用户名或密码错误");
			request.setAttribute("msg", "用户名或密码错误，请重新登录");
			request.getRequestDispatcher("/index.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
